package com.itheima.crops.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.crops.dao.IndexMapper;
import com.itheima.crops.dao.PcropsIndexMapper;
import com.itheima.crops.pojo.Index;
import com.itheima.crops.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private IndexMapper indexMapper;
    @Autowired
    private PcropsIndexMapper pcropsIndexMapper;

    /**
     * 农作物指标全查
     * @return
     */
    @Override
    public List<Index> findAll() {
        List<Index> indexList = indexMapper.selectAll();
        return indexList;
    }

    /**
     * 农作物指标根据id查询
     * @param id
     * @return
     */
    @Override
    public Index findById(Integer id) {
        Index index = indexMapper.selectByPrimaryKey(id);
        if(index == null) {
            throw new RuntimeException("农作物指标不存在");
        }
        return index;
    }

    /**
     * 新增农作物指标
     * @param index
     */
    @Transactional
    @Override
    public void add(Index index) {
        indexMapper.insertSelective(index);
    }

    /**
     * 修改农作物指标
     * @param index
     */
    @Transactional
    @Override
    public void update(Index index) {
        Long id = index.getId();
        Index index1 = indexMapper.selectByPrimaryKey(id);
        if(index1 == null) {
            throw new RuntimeException("农作物指标不存在");
        }
        indexMapper.updateByPrimaryKey(index);
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Index> findPage(Map<String,Object> searchMap, int page, int size){
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Index>)indexMapper.selectByExample(example);
    }

    /**
     * 删除农作物指标
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id) {
        Index index = indexMapper.selectByPrimaryKey(id);
        if(index == null) {
            throw new RuntimeException("农作物指标不存在");
        }

        List<Long> cids = pcropsIndexMapper.findCidsByIid(id);
        if(cids != null && cids.size() > 0) {
            throw new RuntimeException("该农作物指标与农作物关联");
        }

        List<Long> deviceIds = indexMapper.findDeviceIdsByIid(id);
        if(deviceIds != null && deviceIds.size() > 0) {
            throw new RuntimeException("该农作物指标与设备关联");
        }

        indexMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Index.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 主键
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            // 指标名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name", "%"+searchMap.get("name")+"%");
            }
            // 计量单位
            if(searchMap.get("unit")!=null && !"".equals(searchMap.get("unit"))){
                criteria.andLike("unit","%"+searchMap.get("unit")+"%");
            }
        }
        return example;
    }
}
